package com.service;

import com.entity.Admin;
import com.mapper.AdminMapper;

import com.utils.JsonUtil;
import com.utils.SqlSessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import java.util.List;


public class AdminService implements AdminMapper {
    SqlSessionFactory factory = SqlSessionFactoryUtils.getSqlSessionFactory();

    JsonUtil jsonUtil = new JsonUtil();

    /**
     * 查找所有用户
     * @return Admin集合
     */
    @Override
    public List<Admin> findAllAdmin() {
        SqlSession sqlSession = factory.openSession();
        AdminMapper mapper = sqlSession.getMapper(AdminMapper.class);
        List<Admin> allAdmin = mapper.findAllAdmin();
        return allAdmin;
    }

    /**
     * 根据用户名进行模糊查找
     * @param name
     * @return Admin集合
     */
    @Override
    public List<Admin> findAdminByName(String name) {
        SqlSession sqlSession = factory.openSession();
        AdminMapper mapper = sqlSession.getMapper(AdminMapper.class);
        List<Admin> adminByName = mapper.findAdminByName(name);
        return adminByName;
    }

    /**
     * 根据用户ID查找用户
     * @param id
     * @return Admin对象
     */
    @Override
    public Admin findAdminById(int id) {
        SqlSession sqlSession = factory.openSession();
        AdminMapper mapper = sqlSession.getMapper(AdminMapper.class);
        Admin adminById = mapper.findAdminById(id);
        return adminById;
    }

    /**
     * 根据邮箱进行模糊查找
     * @param email
     * @return Admin集合
     */
    @Override
    public List<Admin> findAdminByEmail(String email) {
        SqlSession sqlSession = factory.openSession();
        AdminMapper mapper = sqlSession.getMapper(AdminMapper.class);
        List<Admin> adminByEmail = mapper.findAdminByEmail(email);
        return adminByEmail;
    }

    /**
     * 实时查询该用户是否存在
     * @param name 用户名
     * @return 返回查询用户名结果
     * 查询结果为空，则返回null
     */
    @Override
    public String findAdminName(String name) {
        SqlSession sqlSession = factory.openSession();
        AdminMapper mapper = sqlSession.getMapper(AdminMapper.class);
        String adminName = mapper.findAdminName(name);

        return adminName;
    }

    /**
     * 查找用户总数
     * @return int 返回用户整数
     */
    @Override
    public int count() {
        SqlSession sqlSession = factory.openSession();
        AdminMapper mapper = sqlSession.getMapper(AdminMapper.class);
        int count = mapper.count();
        return count;
    }

    @Override
    public List<Admin> findAdminPage(int pageNum, int offset) {
        int pageSize= (pageNum-1)*offset; //pageSize start条数
        SqlSession sqlSession = factory.openSession();
        AdminMapper mapper = sqlSession.getMapper(AdminMapper.class);
        List<Admin> admin = mapper.findAdminPage(pageSize,offset);
        sqlSession.close();
        return admin;
    }

    /**
     * 添加用户
     * @param admin
     * @return 受影响的行数
     */
    @Override
    public int addAdmin(Admin admin) {
        SqlSession sqlSession = factory.openSession();
        AdminMapper mapper = sqlSession.getMapper(AdminMapper.class);
        int rows = mapper.addAdmin(admin);
        sqlSession.commit();
        return rows;
    }

    /**
     * 更新用户资料(不包括密码)
     * @param name 用户名
     * @param avatar 用户头像
     * @param email 用户邮箱
     * @param id 用户ID
     * @return 受影响的行数
     */
    @Override
    public int updateAdminById(int id,String name, String avatar, String email) {
        SqlSession sqlSession = factory.openSession();
        AdminMapper mapper = sqlSession.getMapper(AdminMapper.class);
        int rows = mapper.updateAdminById(id, name, avatar, email);
        sqlSession.commit();
        return rows;
    }

    /**
     * 根据ID查询密码
     * @param id 用户id
     * @return 返回查询到的密码
     */
    @Override
    public String findPdById(int id) {
        SqlSession sqlSession = factory.openSession();
        AdminMapper mapper = sqlSession.getMapper(AdminMapper.class);
        String pdById = mapper.findPdById(id);
        return pdById;
    }

    /**
     * 根据ID修改用户密码
     * @param newPassword 用户新密码
     * @param id 用户ID
     * @return 受影响的行数
     */
    @Override
    public int updateAdminPd(String newPassword, int id) {
//        String pdById = findPdById(id);
//        if (pdById.equals(oldpassword)){
//            //密码相等 修改后返回true
//        }else {
//            //密码不相等，则返回false
//        }

        SqlSession sqlSession = factory.openSession();
        AdminMapper mapper = sqlSession.getMapper(AdminMapper.class);
        int rows = mapper.updateAdminPd(newPassword, id);
        sqlSession.commit();
        return rows;
    }

    /**
     * 根据ID删除用户
     * @param id 用户ID
     * @return 受影响的行数
     */
    @Override
    public int delAdminById(int id) {
        SqlSession sqlSession = factory.openSession();
        AdminMapper mapper = sqlSession.getMapper(AdminMapper.class);
        int b = mapper.delAdminById(id);
        sqlSession.commit();
        return b;
    }

    public boolean editAdminPd(int id,String oldPd,String newPd){
        String pd = findPdById(id);
        if (pd.equals(oldPd)){
            //相等
            int i = updateAdminPd(newPd, id);
            System.out.println("密码已更新成功");
            return true;

        }else {
            System.out.println("密码更新失败");
            return false;
        }
    }
}
